﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Project.Tool
{
	public class upFile
	{
		/// <summary>
		/// 允许上传文件后缀名
		/// </summary>
		private SortedList listDis { get; set; }

		/// <summary>
		/// 文件名称
		/// </summary>
		private string fileName { get; set; }

		/// <summary>
		/// 文件上传路径
		/// </summary>
		private string filePath { get; set; }

		/// <summary>
		/// 实例上传
		/// </summary>
		/// <param name="_listDis">允许上传文件后缀名</param>
		/// <param name="_fileName">文件名称</param>
		/// <param name="_filePath">上传文件路径</param>
		public upFile(SortedList _listDis, string _fileName, string _filePath) {

			this.listDis = _listDis;

			this.fileName = _fileName;

			this.filePath = _filePath;

		}
		/// <summary>
		/// 上传文件
		/// </summary>
		/// <param name="fileList">需要上传文件</param>
		/// <param name="fileType">文件类型[默认Image]</param>
		/// <param name="ComPress">文件压缩</param>
		/// <returns></returns>
		public List<string> UpLoadFile(HttpFileCollectionBase fileList,string fileType = "Image",bool ComPress = false)
		{
			List<string> fileName = new List<string>();

			for (int i = 0; i < fileList.Count; i ++) {

                fileName.Add(UpLoadFile(fileList[i], this.filePath,fileType,ComPress, i));

            }

			return fileName;
		}

		public List<string> UpLoadFile(HttpFileCollection fileList, string fileType = "Image", bool ComPress = false)
		{
			List<string> fileName = new List<string>();

			for ( int i = 0; i < fileList.Count; i++ )
			{

				fileName.Add(UpLoadFile(fileList[i], this.filePath, fileType, ComPress, i));

			}

			return fileName;
		}

		private string UpLoadFile(HttpPostedFile httpFile, string toFilePath, string type, bool ComPress, int i = 0)
		{
			try
			{

				string 
					//文件名
					filerealname = httpFile.FileName,

					//文件后缀名
					fileNameExt = Path.GetExtension(filerealname).ToLower(),

					//年月日
					datePath = DateTime.Now.ToString("yyyyMMdd") + "/",
					
					//获取服务器路径
					serverPath = HttpContext.Current.Request.PhysicalApplicationPath,

					//文件物理保存路径
					filePath = toFilePath + datePath,

					//保存的文件名
					fileName = this.fileName + i + fileNameExt,

					//文件服务器保存路径
					serverFilePath = serverPath + toFilePath + datePath;

				//检查后缀名是否合法
				if ( listDis.Contains(fileNameExt) || listDis.Count.Is(0))
				{
					//检测目录是否存在 不存在则创建
					if ( !Directory.Exists(serverFilePath) ) Directory.CreateDirectory(serverFilePath);

					//拼接文件名
					string
						saveAsFileName = serverFilePath + fileName;

					//保存到服务器
					httpFile.SaveAs(saveAsFileName);

					//是否压缩
					if ( ComPress )
					{
						switch ( type )
						{
							case "Image":
                                AsyncHelper.NewThread(() => { new Img.ComPress(filePath, Prototype.SizeType(fileName, datePath)).Start(); }).Start();
								break;

							case "Vedio":
                                AsyncHelper.NewThread(() => { new Vedio.Preview().Img(saveAsFileName, serverPath); }).Start();
								break;

                            default:
								break;
						}
					}

					return filePath + fileName;
				}
				else
				{
					return "文件不合法";
				}
			}
			catch ( Exception e )
			{
				return e.Message;
			}
		}

		public string UpLoadFile(HttpPostedFileBase httpFile, string toFilePath, string type, bool ComPress, int i = 0)
		{
			try
			{
				
				string 
					//文件名
					filerealname = httpFile.FileName,

					//文件后缀名
					fileNameExt = Path.GetExtension(filerealname).ToLower(),

					//年月日
					datePath = DateTime.Now.ToString("yyyyMMdd") + "/",
					
					//获取服务器路径
					serverPath = HttpContext.Current.Request.PhysicalApplicationPath,

					//文件物理保存路径
					filePath = toFilePath + datePath,

					//保存的文件名
					fileName = this.fileName + i + fileNameExt,

					//文件服务器保存路径
					serverFilePath = serverPath + toFilePath + datePath;

				//检查后缀名是否合法
				if ( listDis.Contains(fileNameExt) || listDis.Count.Is(0))
				{
					//检测目录是否存在 不存在则创建
					if ( !Directory.Exists(serverFilePath) ) Directory.CreateDirectory(serverFilePath);

					//拼接文件名
					string
						saveAsFileName = serverFilePath + fileName;

					//保存到服务器
					httpFile.SaveAs(saveAsFileName);

					//是否压缩
					if ( ComPress )
					{
						switch ( type )
						{
                            case "Image":
                                AsyncHelper.NewThread(() => { new Img.ComPress(filePath, Prototype.SizeType(fileName, datePath)).Start(); }).Start();
                                break;

                            case "Vedio":
                                AsyncHelper.NewThread(() => { new Vedio.Preview().Img(saveAsFileName, serverPath); }).Start();
                                break;

                            default:
								break;
						}
					}
					
					return filePath + fileName;
				}
				else
				{
					return "文件不合法";
				}
			}
			catch ( Exception e )
			{
				return e.Message;
			}
		}
	}
}
